package de.bbl.android.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import de.bbl.android.BBLAndroidFragmentActivity;
import de.bbl.android.R;
import de.bbl.android.fragment.DataCenter.DataCenterFragment;
import de.bbl.android.fragment.GameCenter.GameCenterFragment;
import de.bbl.android.fragment.NewsCenter.NewsCenterFragment;
import de.bbl.android.fragment.VideoCenter.VideoCenterFragment;

public class TabFragment extends Fragment {

	private static final int GAMECENTER = 0x1;
    private static final int DATACENTER = 0x2;
    private static final int NEWSCENTER = 0x3;
    private static final int VIDEOCENTER = 0x4;
    
    private FragmentManager fm = null;
    private FragmentTransaction ft= null;
    private TabHost currentTabHost = null;
    
    private NewsCenterFragment currentNewsCenterFragment = null;
    private GameCenterFragment currentGameCenterFragment = null;
    private DataCenterFragment currentDataCenterFragment = null;
    private VideoCenterFragment currentVideoCenterFragment = null;
    
    private int mTabState = 0x0;
    private int reloadTabState = 0x0;
    
    // 0x1 = PORTRAIT
    // 0x2 = LANDSCAPE
    private int ORIENTATION_STATE = 0xF;
    
	boolean mDualPane;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	//TODO: refactoring this method
    	if (savedInstanceState != null) {
    		reloadTabState = savedInstanceState.getInt("curTab");
    		ORIENTATION_STATE = savedInstanceState.getInt("curOrientation");
    	}
    	
    	int compare = getResources().getConfiguration().orientation;
    	if (ORIENTATION_STATE == 0xF)
    		ORIENTATION_STATE = compare;
    	
        View view = inflater.inflate(R.layout.fragment_tab, container, false);

        final TabHost tab_host = (TabHost) view.findViewById(R.id.tabhost);
        tab_host.setup(); 

        TabSpec ts1 = tab_host.newTabSpec("TAB_GAMES");
        ts1.setIndicator("Games");
        ts1.setContent(R.id.tab_games);
        tab_host.addTab(ts1);
        
        TabSpec ts2 = tab_host.newTabSpec("TAB_DATA");
        ts2.setIndicator("Datencenter");
        ts2.setContent(R.id.tab_data);
        tab_host.addTab(ts2);

        TabSpec ts3 = tab_host.newTabSpec("TAB_NEWS");
        ts3.setIndicator("News");
        ts3.setContent(R.id.tab_news);
        tab_host.addTab(ts3);

        TabSpec ts4 = tab_host.newTabSpec("TAB_VIDEO");
        ts4.setIndicator("Videos");
        ts4.setContent(R.id.tab_video);
        tab_host.addTab(ts4);

        fm = getFragmentManager();
        ft = fm.beginTransaction();

        if (reloadTabState != 0x0) {
        	tab_host.setCurrentTab(reloadTabState - 1);
        	currentGameCenterFragment = (GameCenterFragment)fm.findFragmentByTag("gamecenter");
        	currentDataCenterFragment = (DataCenterFragment)fm.findFragmentByTag("datacenter");
        	currentNewsCenterFragment = (NewsCenterFragment)fm.findFragmentByTag("newscenter");
        	currentVideoCenterFragment = (VideoCenterFragment)fm.findFragmentByTag("videocenter");
        	System.out.println("reloadTab: " + String.valueOf(reloadTabState));
        }

    	if (compare != ORIENTATION_STATE) {
    		fm.popBackStackImmediate();
    	}

        tab_host.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
		        tab_host.setCurrentTab(0);
		        setEmptySideContent();
		        gotoGAMECENTER();
			}
		});
        
        tab_host.getTabWidget().getChildAt(1).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
		        tab_host.setCurrentTab(1);
		        setEmptySideContent();
		        gotoDATACENTER();
			}
		});

        tab_host.getTabWidget().getChildAt(2).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
		        tab_host.setCurrentTab(2);
		        setEmptySideContent();
		        gotoNEWSCENTER();
			}
		});

        tab_host.getTabWidget().getChildAt(3).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
		        tab_host.setCurrentTab(3);
		        setEmptySideContent();
		        gotoVIDEOCENTER();
			}
		});

        currentTabHost = tab_host;
        return view;
    }
    
    private void setEmptySideContent() {
        View sideContent = getActivity().findViewById(R.id.fragment_side_content);
        mDualPane = sideContent != null && sideContent.getVisibility() == View.VISIBLE;
        if (mDualPane) {
    		EmptyCenterFragment extended = new EmptyCenterFragment();
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			ft.replace(R.id.fragment_side_content, extended, "empty-extended");
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			ft.commit();
        }

    }
    
    public void startCenterRepresentation() {
    	switch(reloadTabState) {
    	case DATACENTER:
	        currentTabHost.setCurrentTab(1);
    		gotoDATACENTER();
    		break;
    	case NEWSCENTER:
	        currentTabHost.setCurrentTab(2);
    		gotoNEWSCENTER();
    		break;
    	case VIDEOCENTER:
	        currentTabHost.setCurrentTab(3);
    		gotoVIDEOCENTER();
    		break;
  		default:
	        currentTabHost.setCurrentTab(0);
    		gotoGAMECENTER();
    	}
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curTab", mTabState);
        outState.putInt("curOrientation", ORIENTATION_STATE);
    }

    public void gotoGAMECENTER() {
    	if (mTabState != GAMECENTER) {
            mTabState = GAMECENTER;
			reloadTabState = mTabState;
            
            if (fm != null) {
            	if (currentGameCenterFragment == null)
            		currentGameCenterFragment = new GameCenterFragment();
                ft = fm.beginTransaction();
                ft.replace(R.id.fragment_content, currentGameCenterFragment, "gamecenter");
                ft.addToBackStack(getTag());
                BBLAndroidFragmentActivity.currentFragmentTag = "gamecenter";
                ft.commit();
                currentTabHost.setCurrentTab(0);
            }
        }
    }

    public void gotoDATACENTER() {
    	// if (mTabState != DATACENTER) {
    	// (true for loading Main Screen of tab)
     	if (true) {
    		boolean addToBackStack = true;
            mTabState = DATACENTER;
            if (reloadTabState == DATACENTER)
        		addToBackStack = false;
			reloadTabState = mTabState;
            
            if (fm != null) {
            	// if (currentDataCenterFragment == null)
           		currentDataCenterFragment = new DataCenterFragment();
            	
                ft = fm.beginTransaction();
                ft.replace(R.id.fragment_content, currentDataCenterFragment, "datacenter");
                BBLAndroidFragmentActivity.currentFragmentTag = "datacenter";
                if (addToBackStack)
                    ft.addToBackStack(getTag());
                ft.commit();
                currentTabHost.setCurrentTab(1);
            }
        }
    }

    public void gotoNEWSCENTER() {
    	if (mTabState != NEWSCENTER) {
    		boolean addToBackStack = true;
            mTabState = NEWSCENTER;
            if (reloadTabState == NEWSCENTER)
            	addToBackStack = false;
			reloadTabState = mTabState;
            
            if (fm != null) {
            	if (currentNewsCenterFragment == null) {
            		currentNewsCenterFragment = new NewsCenterFragment();
            	}

            	ft = fm.beginTransaction();
           		ft.replace(R.id.fragment_content, currentNewsCenterFragment, "newscenter");
                BBLAndroidFragmentActivity.currentFragmentTag = "newscenter";
                // if (! addToBackStack)
                //	fm.popBackStack();
                if (addToBackStack)
                  ft.addToBackStack(getTag());
                ft.commit();
                currentTabHost.setCurrentTab(2);
            }
        }
    }

    public void gotoVIDEOCENTER() {
    	if (mTabState != VIDEOCENTER) {
    		boolean addToBackStack = true;
            mTabState = VIDEOCENTER;
            if (reloadTabState == VIDEOCENTER)
        		addToBackStack = false;
			reloadTabState = mTabState;
            
            if (fm != null) {
            	if (currentVideoCenterFragment == null)
            		currentVideoCenterFragment = new VideoCenterFragment();
                ft = fm.beginTransaction();
                ft.replace(R.id.fragment_content, currentVideoCenterFragment, "videocenter");
                BBLAndroidFragmentActivity.currentFragmentTag = "videocenter";
                // if (! addToBackStack)
                //	fm.popBackStack();
                // ft.addToBackStack(getTag());
                if (addToBackStack)
                    ft.addToBackStack(getTag());
                ft.commit();
                currentTabHost.setCurrentTab(3);
            }
        }
    }

    public void RefreshData() {
    	switch(mTabState) {
    	case GAMECENTER:
    		if (currentGameCenterFragment != null)
    			currentGameCenterFragment.RefreshData();
    		break;
    	case DATACENTER:
    		if (currentDataCenterFragment != null)
    			currentDataCenterFragment.RefreshData();
    		break;
    	case NEWSCENTER:
    		if (currentNewsCenterFragment != null)
    			currentNewsCenterFragment.RefreshData();
    		break;
  		default:
  			;
    	}
    }

    public void OptionsMenu() {
    	switch(mTabState) {
    	case GAMECENTER:
    		if (currentGameCenterFragment != null) {
    			currentGameCenterFragment.OpenOptionsMenu();
    		}
    		break;
    	case DATACENTER:
    		if (currentDataCenterFragment != null) {
    			currentDataCenterFragment.OpenOptionsMenu();
    		}
    	case 0x3:
    		break;
  		default:
  			;
    	}
    }

    public void setCurrentTab(String currentFragmentTag) {
		if (currentFragmentTag.contains("gamecenter")) {
			currentTabHost.setCurrentTab(0);
			mTabState = GAMECENTER;
		}
		else if (currentFragmentTag.contains("datacenter")) {
			currentTabHost.setCurrentTab(1);
			mTabState = DATACENTER;
		}
		else if (currentFragmentTag.contains("newscenter")) {
			currentTabHost.setCurrentTab(2);
			mTabState = NEWSCENTER;
		}
		else if (currentFragmentTag.contains("videocenter")) {
			currentTabHost.setCurrentTab(3);
			mTabState = VIDEOCENTER;
		}
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
	}

	public int getCurrentTab() {
		return mTabState;
	}

	public void setGameCenter() {
		currentTabHost.setCurrentTab(0);
        setEmptySideContent();
        gotoGAMECENTER();
	}
}
